﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Text;
namespace logicunivesityinventorystore
{
    public partial class ManageRequest : System.Web.UI.Page
    {
        //set here for the employee id from session
        string empid;
        static string reqFormID;
        static string status = "";
        RequisitionFacade reqfac = new RequisitionFacade();
        RetrivalListFacade rlFacade = new RetrivalListFacade();
        ItemFacade ifac = new ItemFacade();
        protected void Page_Load(object sender, EventArgs e)
        {
<<<<<<< .mine
            empid = "A0102962L";// Session["UserName"].ToString();
=======
            empid = User.Identity.Name;
>>>>>>> .r586
            lblempID.Text = empid;
            lblempID.Visible = false;
            lblOrderDetail.Visible = false;
            if (gvReqList.Rows.Count == 0)
            {
                ScriptManager.RegisterStartupScript(this, typeof(string), "Error", "alert('There are no Requests made!');window.location.href = 'HomePage_Store.aspx'", true);
            }
        }

        protected void gvReqList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                gvOrderDetails.DataSource = new RequisitionFacade().getRequsitionDetails(e.CommandArgument.ToString());
                gvOrderDetails.DataBind();
                lblOrderDetail.Visible = true;
            }
            if (e.CommandName == "Remove")
            {
                reqFormID = e.CommandArgument.ToString();
                lbtnReject_ModalPopupExtender.Show();
            }
        }

        protected void btnApprove_Click1(object sender, EventArgs e)
        {
            RetrivalListDetail rl = new RetrivalListDetail();
            RetrivalList retrival = new RetrivalList();
            List<RetrivalList> ret = new List<RetrivalList>();
            string deptCode = new DepartmentFacade().getDepartmentCode(lblempID.Text);
            string retrivalListID = rlFacade.GenerateRetrievalListID(deptCode);
            Item itm;
            string formId;
            List<RetrivalListDetail> rdlList = new List<RetrivalListDetail>();
            if (gvReqList.Rows.Count > 0)
            {
                foreach (GridViewRow gvRow in gvReqList.Rows)
                {
                    CheckBox chkBox = (CheckBox)gvRow.FindControl("chkSelection");
                    if (chkBox.Checked)
                    {
                        LinkButton linkBtn = (LinkButton)gvRow.FindControl("lbtnFormId");
                        formId = linkBtn.Text;
                        foreach (RequisitionDetail req in reqfac.FindItemsByReqFormID(formId))
                        {
                            itm = ifac.FindByItemID(req.ItemCode);
                            itm.LogicStock = itm.LogicStock - req.Qty;
                            if (itm.LogicStock < itm.ReorderLevel)
                            {
                                //send email for under reorder level
                                string clerkId = new DepartmentFacade().FindClerkIdByDeptCode(deptCode);
                                status = "Logic Stock Quantity less than Reorder Level.";
                                reqfac.SendUnderReorderEmail(clerkId, formId, status);
                                new SMSFacade().smsToClerk(empid, req.ItemCode);
                            }
                            ifac.UpdateLogicStock(itm.ItemCode, itm.LogicStock);//work
                            if (rlFacade.CheckRetrivalDetailId(retrivalListID, req.ItemCode))
                            {
                                rl = rlFacade.FindRetrivalListDetailsByIDAndItemCode(retrivalListID, req.ItemCode);
                                int reqQty = reqfac.FindRequisitionDetailsByIDAndItemCode(formId, req.ItemCode).Qty;
                                rlFacade.UpdateNeededQty(retrivalListID, req.ItemCode, reqQty);
                            }
                            else
                            {
                                rl.RetrivalListID = retrivalListID;
                                rl.ItemCode = req.ItemCode;
                                rl.NeededQty = req.Qty;
                                rl.Remark = "Processing";
                                rdlList.Add(rl);
                            }
                        }
                        rlFacade.InsertRetrivalListDetail(rdlList);
                        reqfac.ApprovedRequisitionStatusById(formId);//work     
                        gvReqList.DataBind();
                        //status = "Approved";
                        //reqfac.SendApprovedEmail(lblempID.Text, formId, status);//work
                    }
                    //else
                    //{
                    //    ScriptManager.RegisterStartupScript(this, typeof(string), "Warning!", "alert('Select at least one checkbox to approve!');window.location.href = 'ManageRequest.aspx'", true);
                    //}
                }
            }
            gvReqList.DataBind();
          
        }

        protected void btnApproveAll_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow gvRow in gvReqList.Rows)
            {
                CheckBox chkBox = (CheckBox)gvRow.FindControl("chkSelection");
                chkBox.Checked = true;
            }
            btnApprove_Click1(sender, e);
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (gvReqList.Rows.Count > 0)
            {
                string remark = txtHeadRemarks.Text;
                if (remark != null)
                {
                    reqfac.RejectRequisitionForm(reqFormID, remark);
                    status = "Rejected";
                    reqfac.SendApprovedEmail(lblempID.Text, reqFormID, status);
                    gvReqList.DataBind();
                }
            }
        }

        protected void gvReqList_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }   
    }
}

